﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FWMS.COMMAND;

namespace FWMS
{
    public partial class Form_userManagement : Form
    {
        public Form_userManagement()
        {
            InitializeComponent();
        }

        private void bn_ok_Click(object sender, EventArgs e)
        {
            //用户名出入验证
            if ("".Equals(COMMAND.Command.nullToSpace(this.tb_username.Text).Trim()))
            {
                MessageBox.Show("用户名不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_username.Focus();
                return;
            }

            //原密码出入验证
            if ("".Equals(COMMAND.Command.nullToSpace(this.tb_passwordOld.Text).Trim()))
            {
                MessageBox.Show("原密码不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_passwordOld.Focus();
                return;
            }

            //新密码出入验证
            if ("".Equals(COMMAND.Command.nullToSpace(this.tb_password_new.Text).Trim()))
            {
                MessageBox.Show("新密码不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_password_new.Focus();
                return;
            }

            //新密码出入验证
            if ("".Equals(COMMAND.Command.nullToSpace(this.tb_passwordNew_confirm.Text).Trim()))
            {
                MessageBox.Show("新密码确认不能为空！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_passwordNew_confirm.Focus();
                return;
            }

            if (tb_password_new.Text.Length != 6)
            {
                MessageBox.Show("新密码必须为6位数！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_password_new.Text = "";
                this.tb_password_new.Focus();
                return;
            }

            if (!COMMAND.Command.nullToSpace(this.tb_password_new.Text).Trim().Equals(COMMAND.Command.nullToSpace(this.tb_passwordNew_confirm.Text).Trim()))
            {
                MessageBox.Show("新密码和新密码确认不一致！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_passwordNew_confirm.Text = "";
                this.tb_passwordNew_confirm.Focus();
                return;
            }

            Service.UserService userservice = new Service.UserService();

            Bean.UserBean bean = new Bean.UserBean();
            bean.Username = tb_username.Text.Trim();
            bean.Password = tb_passwordOld.Text.Trim();

            //查询数据库
            bean = userservice.userServiceSelect(bean);
            if (bean.Username != null && bean.Password != null)
            {
                bean.Username = this.tb_username.Text;
                bean.Password = this.tb_password_new.Text;
                bool modify_password = userservice.userSerivceModify(bean);
                if (modify_password)
                {
                    MessageBox.Show("密码修改成功！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("密码修改失败,请尝试重新修改！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                this.tb_passwordOld.Text = "";
                MessageBox.Show("原密码密码输入有误,请重新输入！", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.tb_passwordOld.Focus();
            }

        }

        private void bn_cannel_Click(object sender, EventArgs e)
        {
            this.tb_username.Text = "";
            this.tb_passwordOld.Text = "";
            this.tb_password_new.Text = "";
            this.tb_passwordNew_confirm.Text = "";
            this.tb_username.Focus();
        }

    }
}
